package com.kai.steering.data.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.vo.ReturnCodeExt;
import com.kai.steering.data.service.entity.domainObject.QuestionDO;
import com.kai.steering.data.service.entity.domainObject.QuestionDetailsDO;
import com.kai.steering.data.service.entity.domainObject.UserQuestionDO;
import com.kai.steering.data.service.entity.po.Question;
import com.kai.steering.data.service.entity.po.QuestionDetails;
import com.kai.steering.data.service.entity.vo.QuestionVO;
import com.kai.steering.data.service.mapper.QuestionDetailsMapper;
import com.kai.steering.data.service.service.QuestionDetailsService;
import com.kai.steering.common.web.service.impl.BaseServiceImpl;
import com.kai.steering.common.vo.Page;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;

/**
 * <pre>
 * 题目详情 服务实现类
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@Service
public class QuestionDetailsServiceImpl extends BaseServiceImpl<QuestionDetailsMapper, QuestionDetails> implements QuestionDetailsService {

    @Autowired
    private QuestionDetailsMapper questionDetailsMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public QuestionDetailsDO saveQuestionDetails(QuestionDetailsDO questionDetailsDO) {
        //校验参数
        if (questionDetailsDO.getQuestionId()==null) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象questionDetailsDO[" + "questionId" + "]不存在");
        }
        //数据准备
        QuestionDetails questionDetails = new QuestionDetails(questionDetailsDO);
        questionDetails.setCreationDatetime(new Date());

        boolean save = super.save(questionDetails);
        if (save){
            return new QuestionDetailsDO(questionDetails);
        }else {
            return null;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateQuestionDetails(Serializable id, QuestionDetailsDO questionDetailsDO)
    {
        questionDetailsDO.setQuestionDetailsId(id.toString());
        QuestionDetailsDO questionDetailsById = getQuestionDetailsById(id);
        if (questionDetailsById==null||questionDetailsById.getQuestionDetailsId()==null){
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象questionDetailsDO[" + "id"+id + "]不存在");
        }
        QuestionDetails questionDetails = new QuestionDetails(questionDetailsDO);
        return super.updateById(questionDetails);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteQuestionDetails(Serializable id) {

        super.removeById(id);
        return true;
    }

    @Override
    public QuestionDetailsDO getQuestionDetailsById(Serializable id) {
        QuestionDetails questionDetails = super.getById(id);

        QuestionDetailsDO questionDetailsDO = new QuestionDetailsDO(questionDetails);
        return questionDetailsDO;
    }
    @Override
    public  QuestionDetailsDO getQuestionDetailsByQId(Serializable id){
        QueryWrapper<QuestionDetails> qw = new QueryWrapper<>();
        qw.eq("question_id",id);
        QuestionDetails questionDetails = super.getOne(qw);
        QuestionDetailsDO questionDetailsDO = new QuestionDetailsDO(questionDetails);
        return questionDetailsDO;
    }

    @Override
    public List<QuestionDetailsDO> QuestionDetailsDO() {
        List<QuestionDetails> questionDetailslist = questionDetailsMapper.selectList(new QueryWrapper<QuestionDetails>());
        List<QuestionDetailsDO> result = questionDetailslist.stream()
                .map(questionDetails -> {
                    QuestionDetailsDO questionDetailsVo = new QuestionDetailsDO();
                    BeanUtil.copyProperties(questionDetails, questionDetailsVo);
                    return questionDetailsVo;
                })
                .collect(Collectors.toList());
        return result;
    }

//    @Override
//    public Page<QuestionDetailsVO> getQuestionDetailsPageList(QuestionDetails queryParam) {
//        com.baomidou.mybatisplus.extension.plugins.pagination.Page page = setPageParam(
//                queryParam, OrderItem.desc("create_time")
//        );
//
//        QueryWrapper<QuestionDetails> queryWrapper = new QueryWrapper<>();
//
//        //组合条件
//        IPage<QuestionDetails> entityPage = questionDetailsMapper.selectPage(page, queryWrapper);
//        List<QuestionDetailsVO> result = entityPage.getRecords().stream()
//                .map(questionDetails -> {
//                    QuestionDetailsVO questionDetailsVo = new QuestionDetailsVO();
//                    BeanUtil.copyProperties(questionDetails, questionDetailsVo);
//                    return questionDetailsVo;
//                })
//                .collect(Collectors.toList());
//
//        return Page.<QuestionDetailsVO>builder()
////                .currPage(queryParam.getPageNo())
////                .pageSize(queryParam.getPageSize())
//                .totalCount((int) entityPage.getTotal())
//                .list(result)
//                .build();
//    }


    @Override
    public boolean isExists(String key, String val, Serializable id) {
        QueryWrapper<QuestionDetails> qw = new QueryWrapper<>();
        qw.eq(key, val);
        if (id != null) {
            qw.ne("id", id);
        }
        int num = this.count(qw);
        return num > 0;
    }
    @Override
    public Map<String,QuestionDetailsDO> getQuestionDetailsMap(List<String> collect) {
        if (CollUtil.isEmpty(collect)) {
            return null;
        }
        QueryWrapper<QuestionDetails> qw = new QueryWrapper<>();
        qw.in("question_id", collect);
        List<QuestionDetails> list = super.list(qw);
        List<QuestionDetailsDO> questionDetailsDOS = list.stream().map(question -> new QuestionDetailsDO(question)).collect(Collectors.toList());
        Map<String, QuestionDetailsDO> stringQuestionDetailsDOHashMap = new HashMap<>();
        if (questionDetailsDOS!=null ||questionDetailsDOS.size()>0) {
            // 使用Stream API转换为Map
             stringQuestionDetailsDOHashMap = questionDetailsDOS.stream()
                    .collect(Collectors.toMap(QuestionDetailsDO::getQuestionId, Function.identity()));

        }


        return stringQuestionDetailsDOHashMap;
    }
}

